package com.example.blood_sugar.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.blood_sugar.R;
import com.example.blood_sugar.adapters.BloodSugarChartFragmentAdapter;
import com.example.blood_sugar.adapters.BloodSugarFragmentAdapter;
import com.example.blood_sugar.bean.BSRecord;

import java.util.ArrayList;
import java.util.List;

/**
 * 血糖曲线图界面
 */
public class BloodSugarChartFragment extends BaseFragment {

    private TabLayout tab;
    private ViewPager pager;
    private List<String> list;
    private List<Fragment> mFragments;

    public static BloodSugarChartFragment newInstance(String text){
        Bundle bundle = new Bundle();
        bundle.putString("text",text);
        BloodSugarChartFragment bloodSugarChartFragment = new BloodSugarChartFragment();
        bloodSugarChartFragment.setArguments(bundle);
        return  bloodSugarChartFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_blood_sugar_chart, container, false);

        /*初始化界面*/
        initViews(view);
        /*初始化数据*/
        initData();
        /*设置Adapter*/
        pager.setAdapter(new BloodSugarChartFragmentAdapter(getActivity().getSupportFragmentManager(), list, mFragments));
        /*Tab与ViewPager绑定*/
        tab.setupWithViewPager(pager);
        // 缓存3个界面
        pager.setOffscreenPageLimit(3);

        return view;
    }

    /*初始化数据*/
    private void initData() {
        list = new ArrayList<>();
        // String.format(Locale.CHINA,"第%02d页",i)
        list.add("今天");
        list.add("7天");
        list.add("30天");
        mFragments = new ArrayList<>();
        mFragments.add(BloodSugarChartFragment24Hours.newInstance("todo"));
        mFragments.add(BloodSugarChartFragment7Days.newInstance("todo"));
        mFragments.add(BloodSugarChartFragment30Days.newInstance("todo"));
    }

    /*初始化界面*/
    private void initViews(View view) {
        this.pager = view.findViewById(R.id.blood_sugar_chart_pager);
        this.tab = view.findViewById(R.id.blood_sugar_chart_tab);
    }

    private void updateData() {
        if (mFragments != null) {
            for (int i = 0; i < mFragments.size(); i ++) {
                mFragments.get(i).onResume();
//                        setUserVisibleHint(false);
            }
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        updateData();
    }
}

